package com.sunlands.deskmate.controller;

import com.sunlands.deskmate.sstwds.ContentSecCheck;
import com.sunlands.deskmate.vo.CommonResultMessage;
import com.sunlands.deskmate.vo.response.BusinessResult;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author shixiaopeng
 */
@RestController
@Slf4j
public class CheckController {

    @Autowired
    private ContentSecCheck contentSecCheck;

    @ApiOperation(value = "敏感词校验")
    @PreAuthorize("isAuthenticated()")
    @GetMapping("/check/sensitive")
    public BusinessResult<CommonResultMessage> checkContent(@ApiParam(value = "内容") @RequestParam(value = "content") String content,
                                                            @ApiParam(value = "是否校验特殊字符" ) @RequestParam(value = "isSpecial", required = false, defaultValue = "false") Boolean isSpecial) {
        log.info("content = {}, isSpecial = {}", content, isSpecial);
        if (isSpecial) {
            Boolean conSpecial = isConSpeChar(content);
            if (conSpecial) {
                return BusinessResult.createInstance(CommonResultMessage.CON_SPECIAL);
            }
        }
        if (contentSecCheck.checkSensitiveWords(content)) {
            return BusinessResult.createInstance(CommonResultMessage.CON_SENSITIVE);
        }
        return BusinessResult.createSuccessInstance(null);
    }


    @ApiOperation(value = "敏感词校验")
    @PreAuthorize("isAuthenticated()")
    @PostMapping("/check/sensitive")
    public BusinessResult checkContent(@ApiParam(value = "内容") @RequestBody CheckVO checkVO){
        log.info("checkVO = {}", checkVO);
        String words = contentSecCheck.findSenstiveWords(checkVO.content);
        if (words != null) {
            return BusinessResult.createInstance(CommonResultMessage.CON_SENSITIVE.code, words, null );
        }
        return BusinessResult.createSuccessInstance(null);
    }

    @ApiOperation(value = "敏感词替换")
    @PreAuthorize("isAuthenticated()")
    @PostMapping("/check/sensitive/replace")
    public BusinessResult replaceContent(@ApiParam(value = "内容") @RequestBody CheckVO checkVO){
        log.info("敏感词替换 checkVO = {}", checkVO);
        String words = contentSecCheck.filterSensitiveWords(checkVO.content);
        return BusinessResult.createSuccessInstance(words);
    }

    @ApiOperation(value = "重新构建敏感词树")
    @GetMapping("/reloadWord")
    public BusinessResult reloadWord(){
        contentSecCheck.reloadWord();
        return BusinessResult.createSuccessInstance(null);
    }


    /**
     * 校验特殊字符
     *
     * @param str
     * @return
     */
    private static boolean isConSpeChar(String str) {
        String regEx = "[`~!@#$%^&*()+=|{}':;'\\[\\]<>/?~！@#￥%……&*——+|{}【】‘；：”“’。，、？]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.find();
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    static class CheckVO{
        @ApiModelProperty(value = "内容", required = true)
        private String content;
        @ApiModelProperty(value = "是否校验特殊字符", required = false)
        private Boolean isSpecial;
    }
}
